package com.sky.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.Result;
import com.sky.service.DishService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@Slf4j
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;
    /*
    * 菜品分页查询
    * */
    @GetMapping("/page")
    @ApiOperation(value = "菜品分页查询")
    public Result<Page> page(DishPageQueryDTO dishPageQueryDTO){
        Page<DishDTO> page = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(page);
    }
    /*
    * 根据分类id查询菜品
    * */
    @GetMapping("/list")
    @ApiOperation(value = "根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> dishList = dishService.getByCategory(categoryId);
        return Result.success(dishList);
    }
    /*
    * 新增菜品
    * */
    @PostMapping
    @ApiOperation(value = "新增菜品")
    public Result<String> saveWithFlavor(@RequestBody DishDTO dishDTO){
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }
    /*
    * 菜品起售、停售
    * */
    @PostMapping("/status/{status}")
    @ApiOperation(value = "菜品起售、停售")
    public Result<String> status(@PathVariable Integer status,Long id){
        log.info("菜品起售、停售");
        dishService.setStatus(status,id);

        //将所有以dish_开头的key的缓存数据清理掉
        cleanCache("dish_*");

        return Result.success();
    }
    /*
    * 批量删除菜品
    * */
    @DeleteMapping
    @ApiOperation(value = "批量删除菜品")
    public Result<String> deleteWithFlavor(@RequestParam List<Long> ids){
        log.info("批量删除菜品");
        dishService.deleteWithFlavor(ids);

        //将所有以dish_开头的key的缓存数据清理掉
        cleanCache("dish_*");

        return Result.success();
    }
    /*
    * 根据id查询菜品
    * */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishDTO> getById(@PathVariable Long id){
        DishDTO dishDTO = dishService.getWithFlavor(id);
        return Result.success(dishDTO);
    }
    /*
    * 修改菜品
    * */
    @PutMapping
    @ApiOperation(value = "修改菜品")
    public Result<String> updateWithFlavor(@RequestBody DishDTO dishDTO){
        dishService.updateWithFlavor(dishDTO);

        //将所有以dish_开头的key的缓存数据清理掉
        cleanCache("dish_*");

        return Result.success();
    }

    /*
    * 清理缓存数据
    * */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
